package org.mg.frontend.bean;

import org.mg.core.service.DBPropertyService;

import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author MG
 * @since 18.04.2013
 */
@ManagedBean(name = "applicationState")
@ApplicationScoped
public class ApplicationStateBean {

    @ManagedProperty(value = "#{dbPropertyService}")
    DBPropertyService dbPropertyService;

    ConcurrentMap<String, String> properties;

    @PostConstruct
    public void init() {
        properties = new ConcurrentHashMap<String, String>(dbPropertyService.getAllProperties());
    }

    public String getProperty(String key) {
        return properties.get(key);
    }

    synchronized public void setProperty(String key, String value) {
        properties.put(key, value);
        dbPropertyService.saveProperty(key, value);
    }

    public String getApplicationName() {
        return properties.get(DBPropertyService.APPLICATION_NAME);
    }

    public void setApplicationName(String name) {
        setProperty(DBPropertyService.APPLICATION_NAME, name);
    }

    public void setDbPropertyService(DBPropertyService dbPropertyService) {
        this.dbPropertyService = dbPropertyService;
    }
}
